import axios from 'axios'
import store from '@/store/index.js'
import config_url from './baseUrl'
import { Message } from 'element-ui'
const http = {}

var instance = axios.create({
    timeout: 5000,
    baseURL: config_url.baseUrl,
    validateStatus(status) {
        switch (status) {
            case 400:
                Message.error('请求出错')
                break
            case 401:
                Message.warning({
                    message: '授权失败，请重新登录'
                })
                store.commit('LOGIN_OUT')
                setTimeout(() => {
                    window.location.reload()
                }, 1000)
                return
            case 403:
                Message.warning({
                    message: '拒绝访问'
                })
                break
            case 404:
                Message.warning({
                    message: '请求错误,未找到该资源'
                })
                break
            case 500:
                Message.warning({
                    message: '服务端错误'
                })
                break
        }
        return status >= 200 && status < 300
    }
})

// 添加请求拦截器
instance.interceptors.request.use(
    function (config) {
        // 请求头添加token
        if (store.state.UserToken) {
            config.headers.Authorization = `Bearer ${store.state.UserToken}`
        }
        if (config.url.indexOf('/weatherUrl') !== -1) {
            config.url = config.url.replace('/weatherUrl', '')
            config.baseURL = config_url.weatherUrl
        }
        //加入时间戳，强制请求不缓存
        config._t = Date.parse(new Date()) / 1000;
        return config
    },
    function (error) {
        return Promise.reject(error)
    }
)

// 响应拦截器即异常处理
instance.interceptors.response.use(
    response => {
        return response.data
    },
    err => {
        if (err && err.response) {
        } else {
            err.message = '连接服务器失败'
        }
        // Message.error({
        //     message: err.message
        // })
        return Promise.reject(err.response)
    }
)

http.get = function (url, options) {
    let loading
    if (!options || options.isShowLoading !== false) {
        loading = document.getElementById('ajaxLoading')
        loading.style.display = 'block'
    }
    return new Promise((resolve, reject) => {
        instance
            .get(url, options)
            .then(response => {
                if (!options || options.isShowLoading !== false) {
                    loading = document.getElementById('ajaxLoading')
                    loading.style.display = 'none'
                }
                if (url.includes('/api')) {
                    //该请求是正常向后台请求，而不是第三方请求
                    if (response.code === 1001) {
                        //resolve(response.data) //之后的其他情况，返回data就好了
                        resolve(response)
                    } else {
                        Message.error({
                            message: response.msg
                        })
                        reject(response.msg)
                    }
                } else {
                    //第三方请求直接返回
                    resolve(response)
                }

            })
            .catch(e => {
                console.log(e)
            })
    })
}

http.post = function (url, data, options) {
    let loading
    if (!options || options.isShowLoading !== false) {
        loading = document.getElementById('ajaxLoading')
        loading.style.display = 'block'
    }
    return new Promise((resolve, reject) => {
        instance
            .post(url, data, options)
            .then(response => {
                if (!options || options.isShowLoading !== false) {
                    loading = document.getElementById('ajaxLoading')
                    loading.style.display = 'none'
                }
                if (url.includes('/api')) {
                    //表明是后台接口地址
                    if (response.code === 1001) {
                        resolve(response.data)
                    } else {
                        Message.error({
                            message: response.msg
                        })
                        reject(response.message)
                    }
                } else {
                    //这是第三方接口地址
                    resolve(response)
                }

            })
            .catch(e => {
                console.log(e)
            })
    })
}

export default http
